using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Entities;

namespace MM.Business
{
    class clsNHOM_VTPT
    {
        public clsNHOM_VTPT(string sConn)
	    {
		    SqlHelper.ConnectionString = sConn;
	    }
        #region Private methods
        private NHOM_VTPT GetDataRow(DataRow row)
        {
            NHOM_VTPT objNHOM_VTPT = new NHOM_VTPT();

            try
            {
                if (row["TenNhomVTPT"] != DBNull.Value)
                    objNHOM_VTPT._tenNhomVTPT = ((row["TenNhomVTPT"]).ToString());
            }
            catch { }

            try
            {
                if (row["MaNhomVTPT"] != DBNull.Value)
                    objNHOM_VTPT._maNhomVTPT = ((row["MaNhomVTPT"]).ToString());
            }
            catch { }

            try
            {
                if (row["TenNhomVTPT"] != DBNull.Value)
                    objNHOM_VTPT.TenNhomVTPT = ((row["TenNhomVTPT"]).ToString());
            }
            catch { }

            try
            {
                if (row["MaNhomVTPT"] != DBNull.Value)
                    objNHOM_VTPT.MaNhomVTPT = ((row["MaNhomVTPT"]).ToString());
            }
            catch { }

            return objNHOM_VTPT;
        }
        private List<NHOM_VTPT> GetNHOM_VTPT(DataTable dt)
        {
            List<NHOM_VTPT> lst = new List<NHOM_VTPT>();
            NHOM_VTPT obj;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                obj = this.GetDataRow(dt.Rows[i]);
                lst.Add(obj);
            }
            return lst;
        }
        #endregion Private methods

        public List<NHOM_VTPT> usp_SelectNHOM_VTPT()
        {
            string spName = "select * from NHOM_VTPT ";
            DataTable dt = new DataTable();
            List<NHOM_VTPT> lst = new List<NHOM_VTPT>();
            try
            {
                SqlHelper.OpenConnection();

                if (!SqlHelper.IsHaveTran())
                    dt = SqlHelper.ExecuteToDataTable(spName, CommandType.Text);
                else
                {
                    dt = SqlHelper.ExecuteToDataTable(SqlHelper.Tran, spName, CommandType.Text);
                }
                lst = this.GetNHOM_VTPT(dt);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return lst;
        }

        public int InsertUpdateNHOM_VTPT(string maNhomVTPT, string tenNhomVTPT, string maNhomThamChieuSAP)
        {
            int result = 0;
            try
            {
                SqlHelper.OpenConnection();
                string spName = "usp_InsertUpdateNHOM_VTPT";
                SqlParameter[] par = new SqlParameter[3];
                par[0] = new SqlParameter("@MaNhomVTPT", maNhomVTPT);
                par[1] = new SqlParameter("@TenNhomVTPT", tenNhomVTPT);
                par[2] = new SqlParameter("@MaNhomThamChieuSAP", maNhomThamChieuSAP);

                result = SqlHelper.ExecuteNonQuery(spName, par);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (!SqlHelper.IsHaveTran())
                    SqlHelper.CloseConnection();
            }
            return result;
        }
    }
}
